/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to OPNET Translator
 * Author: EWALTERS
 * Created: 10:25:29 AM, Jul 30, 2007
 * MITRE Corporation
 */
package com.mitre.spmif2opnet;

import com.mitre.spmif2opnet.analysis.AnalysisContext;
import com.mitre.spmif2opnet.analysis.GraphBuilder;
import com.mitre.spmif2opnet.generation.OpnetGraphConverter;
import com.mitre.spmif2opnet.opnet.network.Network;
import com.mitre.spmif2opnet.spmif.ProjectType;

/**
 * This Class is the top-level class for the SPMIF to OPNET format translation
 */
public class spmif2opnet {

	/**
	 * The main entry-point for the application.
	 * @param args Command-line arguments - see Options.java for details
	 */
	public static void main(String[] args) {

		// Grab the options from the CLI
		Options opts = new Options();
		if (!opts.getOptions("spmif2opnet", args)) {
			opts.usage();
			System.exit(-1);
		}
		
		try {
			
			// Read in the XML-based representation
			XMLInterface xml_io = new XMLInterface();
			ProjectType project = xml_io.loadXMLFile(opts.getInputFileName());
			
			// Convert the flat XML-based representation into a series of 
			//  fully resolved adjacency lists, as OPNET can't do cross-task 
			//  references or nested task elements
			// NOTE: We could potentially get away with creating a sub-network
			//  within an OPNET node, but this would complicate mapping the tasks 
			//  onto the logical server nodes.  We should also remember that 
			//  tasks within a network must be flat.
			System.out.println("[INFO] Creating operation graph from S-PMIF...");
			GraphBuilder gbuilder = new GraphBuilder();
			AnalysisContext a_context = new AnalysisContext(opts.getPriorityType(), 
					opts.getProperties());
			a_context.setFileName(opts.getInputFileName());

			// Build the S-PMIF projects stored in the context
			gbuilder.buildGraph(project, a_context);
			
			// Resolve all of the S-PMIF variable references
			a_context.resolveVars();
			
			// After this, we should have all of the projects/scenarios we are going to need, 
			//  and all of the variables will be resolved
						
			// Determine how best to map these graphs onto an OPNET-style network
			System.out.println("[INFO] Inferring OPNET network from operation graph...");
			OpnetGraphConverter conv = new OpnetGraphConverter();
			conv.convert(a_context);
						
			// FUTURE: Generate simple layout information

			// Marshal the Network code back into XML and output
			// NOTE: For now, instead of short-circuiting the conversion process,
			//  the exporter just omits the network from its generation
			Network network = conv.exportTasks(opts.genNetwork());
			xml_io.exportXMLFile(opts.getOutputFileName(), network);
			
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}
}
